package com.yang.booking.controller;

import com.yang.booking.pojo.Student;
import com.yang.booking.pojo.StudentScore;
import com.yang.booking.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class StudentController {
    @Autowired
    private StudentService studentService;

    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("/provider/studnet/queryAll")
    public List<Student> queryAll(@RequestParam(name = "id") String id,
                                  @RequestParam(name = "name") String name,
                                  @RequestParam(name = "birth") String birth,
                                  @RequestParam(name = "sex") String sex
//            ,
//                                  @RequestParam(name = "coursename") String coursename,
//                                  @RequestParam(name = "score") String score
    ){
        return studentService.queryAll(id, name, birth, sex); //, coursename, score
    }

    @PostMapping("/provider/studnet/insertStudent")
//    public Map<String, Object> insertStudent(@RequestParam(name = "name") String name,
//                                             @RequestParam(name = "birth") String birth,
//                                             @RequestParam(name = "sex") String sex){
//
//        Student s = new Student();
//        s.setsName(name);
//        s.setsBirth(birth);
//        s.setsSex(sex);
//        return studentService.insertStudent(s);
//    }

    public Map<String, Object> insertStudent(@RequestBody Student s){

        return studentService.insertStudent(s);
    }

    @PostMapping("/provider/studnet/queryScore")
    public List<StudentScore> queryScore(){
        return studentService.queryScore();
    }

    @PostMapping("/provider/studnet/updateStudent")
    public Map<String, Object> updateStudent(@RequestParam(name = "id") String id,
                                             @RequestParam(name = "name") String name,
                                             @RequestParam(name = "birth") String birth,
                                             @RequestParam(name = "sex") String sex
                                             ){

        System.out.println("参数：" + id + " " + name + " " + birth + " " + sex);

        return studentService.updateStudent(id, name, birth, sex);
    }

    @PostMapping("/provider/studnet/deleteStudent")
    public Map<String, Object> deleteStudent(@RequestParam(name = "id") String id) throws InterruptedException {

        System.out.println("参数：" + id);

        return studentService.deleteStudent(id);
    }

    //redis 列表
    @PostMapping("/provider/studnet/list")
    public Map<String, Object> listTest(@RequestParam(name = "student") String student){

        System.out.println("student" + student);

        ListOperations<String,String> listOperations = redisTemplate.opsForList();
        listOperations.leftPush("list",student);

        Map<String, Object> msg = new HashMap<String, Object>();
        msg.put("massage", "插入成功");
        msg.put("code", 200);

        return msg;
    }
}
